﻿var prizeTypeJson = null;
var prizeJson = null;
var typeID = 0;
$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        datatype: "json",
        url: "../../Ajax/InitialSettingAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblPrizeId' id='" + v.Id + "' dir='" + v.PrizeTypeID + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInitialSettingcount' id='" + v.PrizeId + "' >";
                    tbody += v.InitialSettingcount;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/PrizeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                prizeTypeJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#btnAdd").click(function () {
        Edit(null);
    })
})
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblPrizeId]").first();
    Edit(lbl);
}
function InitSelect(value) {
    var sel = "";
    $.each(prizeTypeJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value) {
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
            typeID = v.ID;
        }
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function ChangePrize(value) {
    var value = $("#selprizetypeName").find("option:selected").attr("value");
    $("#selPrizeName").empty().append(InitPrizeSelect(value));
}

function InitPrizeSelect(typeID, value) {
    var sel = "";
    json = PrizeTypeChanged(typeID)
    $.each(json, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function PrizeTypeChanged(typeID) {
    var json = null;
    $.ajax({
        type: "post",
        data: { action: "GetCondtionListByTypeID", typeID: typeID },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        async: false,
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                json = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    return json;
}

function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var Id = $(lbl).parent().parent().find("lable[name=lblPrizeId]").attr("id");
    var typeIDAdd = $(lbl).parent().parent().find("lable[name=lblInitialSettingcount]").attr("id");
    var PrizeTypeID = $(lbl).parent().parent().find("lable[name=lblPrizeId]").attr("dir");
    var content = "";
    if (lbl != null) {
        content = "<table><tr><td>物品类型：<select id='selprizetypeName' onchange='ChangePrize()'>" + InitSelect(PrizeTypeID) + "</select><select id='selPrizeName'>" + InitPrizeSelect(PrizeTypeID) + "</select></td></tr>"
        content += "<tr><td>初始条数：<input type='text' id='txtInitialSettingcount' value='" + $(lbl).parent().parent().find("lable[name=lblInitialSettingcount]").text() + "' style='width:200px' /></td></tr></table>"
    }
    else {
        content = "<table><tr><td>物品类型：<select id='selprizetypeName' onchange='ChangePrize()'>" + InitSelect() + "</select><select id='selPrizeName'>" + InitPrizeSelect(typeID) + "</select></td></tr>"
        content += "<tr><td>初始条数：<input type='text' id='txtInitialSettingcount' style='width:200px' /></td></tr></table>"
    }

    $.dialog({
        title: "更新初始模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#selPrizeName"), Id);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId) {
    var Id = sortId;
    var prizeID = $(sortNum).find("option:selected").attr("value");
    var prizeName = $(sortNum).find("option:selected").text();
    var InitialSettingcount = $(sortNum).parent().parent().parent().find("#txtInitialSettingcount").val();
    var data = contr == null ? { action: "Add", prizeID: prizeID, InitialSettingcount: InitialSettingcount} :
              { action: "Update", id: Id, prizeID: prizeID, InitialSettingcount: InitialSettingcount };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/InitialSettingAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {

            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr>";
                tbody += "<td><lable name='lblprizeID'>";
                tbody += prizeName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblInitialSettingcount'>";
                tbody += InitialSettingcount;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblPrizeId]").first().text(prizeName);
                $(contr).parent().parent().find("lable[name=lblInitialSettingcount]").first().text(InitialSettingcount);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblPrizeId]").first().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/InitialSettingAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(contr).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}